package net.web.belerofont.controller;

import java.util.LinkedList;
import java.util.List;

import net.web.belerofont.action.Interceptor;
import net.web.belerofont.action.Invoker;
import net.web.belerofont.action.InvokerContext;
import net.web.belerofont.action.URIMap;
import net.web.belerofont.action.support.InvokerSupport;

import org.apache.log4j.Logger;

public class InterceptFlow {

	public final Logger LOG = Logger.getLogger(InterceptFlow.class);

	private ConfigReader config;
 
	public static InterceptFlow instance(ConfigReader config) {
		InterceptFlow flow = new InterceptFlow();
		flow.config = config;
		return flow;
	}
	 
	public List<Interceptor> getInterceptors(String actionPath) {
		return config.getInterceptors(actionPath);
	}
	 
	public List<Interceptor> getGlobalInterceptors() {
		return config.getGlobalInterceptors();
	}
	 
	public List getLocalInterceptors(String actionPath) {
		return config.getLocalInterceptors(actionPath);
	}
	 
	public URIMap invoke(InvokerContext context) {
		
		LinkedList<Interceptor> list = new LinkedList<Interceptor>();
			
		list.addAll(getInterceptors(context.getHttpRequest().getRequestURI()));
		
		Invoker invoke = InvokerSupport.instance(context, list);
		
		return invoke.invoke();
	 
	}
	 
}
 
